package br.indt.framework.ui.common;

import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import br.indt.framework.log.MicrologWrapper;
import br.indt.framework.ui.ImageLoader;

/**
 * Button widget
 * 
 * @author Nokia Institute of Technology
 *
 */
public class Button extends Widget
{
	private static final MicrologWrapper log = MicrologWrapper.getLogger(Button.class);
	
	protected Label label = null;
	
	protected Image leftSideImage = null;
	protected Image middleSlice = null;
	protected Image rightSideImage = null;
	
	protected Image leftSideImageClicked = null;
	protected Image middleSliceClicked = null;
	protected Image rightSideImageClicked = null;
	
	protected Image leftSideImageDisable = null;
	protected Image middleSliceDisable = null;
	protected Image rightSideImageDisable = null;
	
	protected Image leftBorderBackgroundImage = null;
	protected Image middleBorderBackgroundImage = null;
	protected Image rightBorderBackgroundImage = null;
	
	protected Image leftBorderBackgroundImageClicked = null;
	protected Image middleBorderBackgroundImageClicked = null;
	protected Image rightBorderBackgroundImageClicked = null;
	
	protected Image selectedBackground;
	
	public int heightImage = 0;
	
	protected boolean layoutTextField = false;

	protected int textColorButtonDefault = 0x434343;
	protected int textColorButtonSelected = 0xf0f1f2;
	protected int textColorButtonDisable = 0x9b9b9b;
	
	protected Icon icon = null;
	
	protected ButtonActionListener buttonActionListener = null;
	protected FocusListener focusListener = null;
	
	private boolean isSelected = false;
	
	public Button(String name, String text, Icon icon)
	{
		super(name);
		
		this.icon = icon;
		
		try 
		{
			setImageLayoutButton();
			label = new Label(name, text, false, 1, null);
			label.setFont(Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN, Font.SIZE_MEDIUM));
			int margin =  ((leftSideImage.getHeight() - label.getFont().getHeight()) / 2);
			label.setMargin(20, margin, 5, 7);
		}
		catch (Exception e)
		{
			log.error("Constructor - " + e);
		}
		calculateHeight();
	}
	
	
	public boolean isLayoutTextField() {
		return layoutTextField;
	}

	public void setLayoutTextField(boolean layoutTextField) {
		this.layoutTextField = layoutTextField;
	}
	
	public ButtonActionListener getButtonActionListener()
	{
		return buttonActionListener;
	}
	
	public void setButtonActionListener(ButtonActionListener buttonActionListener)
	{
		this.buttonActionListener = buttonActionListener;
	}
	
	public void setTextColorButton(int color)
	{
		textColorButtonDefault = color;
	}
	
	public void addFocusListener(FocusListener listener)
	{
		this.focusListener = listener;
	}
	
	public Icon getIcon()
	{
		return icon;
	}
	
	public void setButtonWidth(int width)
	{
		this.width = width;
//		calculateHeight();
	}
	
	
	public void setMargin(int left, int top, int right, int bottom)
	{
		super.setMargin(left, top, right, bottom);
		calculateHeight();
		calculateDimensions();
	}
	
	public void calculateHeight() 
	{		
		height = topMargin + bottomMargin;
		height += heightImage;		
	}
	
	public int getHeight() {
		return height;
	}
	
	public void setWidth(int width) 
	{
		label.setWidth(width);
	}

	public String getText()
	{
		return label.getText();
	}
	
	public void setText(String text)
	{
		label.text = text;
	}
	
	public void setSelectedBackground(Image selectedBackground)
	{
		this.selectedBackground = selectedBackground;
	}

	public  void paint(Graphics g, int x, int y, int maxWidth, int maxHeight){
		try{
			x += leftMargin;
			y += topMargin;
			
			g.setClip(x, y, maxWidth, height);
			
			isSelected = Display.isTouchAndType() ? pressedDown : isSelected();
			
			if (backgroundImage == null) {
				
				
				if (icon != null) {
					icon.setSelected(isSelected);
				}
				
				if (isDisabled()) {
					this.setBackgroundButton(g, x, y,this.leftSideImageDisable, this.middleSliceDisable,this.rightSideImageDisable, textColorButtonDisable);

				}else if (isSelected) {
					if (this.focusListener != null) {
						this.focusListener.focusGained(this);
					}
					this.setBackgroundButton(g, x, y,this.leftSideImageClicked, this.middleSliceClicked,this.rightSideImageClicked, textColorButtonSelected);

				} else  {
					if (this.focusListener != null) {
						this.focusListener.focusLost(this);
					}
					this.setBackgroundButton(g, x, y, this.leftSideImage,this.middleSlice, this.rightSideImage,textColorButtonDefault);
				}
		
				if (icon != null){
					label.paint(g, x + icon.getWidth() + 10, y, maxWidth, maxHeight);
				}else{
					label.paint(g, x, y, maxWidth, maxHeight);
				}
				
			} else {
				g.setClip(x, y - topMargin, maxWidth, heightImage);
				g.drawImage((isSelected && selectedBackground != null) ? selectedBackground : backgroundImage, x, y, Graphics.TOP | Graphics.LEFT);

				if (selectedBackground == null) {

					if (isSelected) {
						this.setBorderButtonImage(g, x, y,this.leftBorderBackgroundImageClicked,this.middleBorderBackgroundImageClicked,this.rightBorderBackgroundImageClicked);
					} else {
						this.setBorderButtonImage(g, x, y,this.leftBorderBackgroundImage,this.middleBorderBackgroundImage,this.rightBorderBackgroundImage);
					}
				}
			}
		}
		catch (Exception e) {
			log.error("paint - " + e.getMessage());
		}
	}
	
	
	protected void setImageLayoutButton(){
		try {
			leftBorderBackgroundImage = ImageLoader.load("/icon/button/img_border_left_default.png");
			middleBorderBackgroundImage = ImageLoader.load("/icon/button/img_border_repeater_default.png");
			rightBorderBackgroundImage = ImageLoader.load("/icon/button/img_border_right_default.png");
			
			leftBorderBackgroundImageClicked = ImageLoader.load("/icon/button/img_border_left_selected.png");
			middleBorderBackgroundImageClicked = ImageLoader.load("/icon/button/img_border_repeater_selected.png");
			rightBorderBackgroundImageClicked = ImageLoader.load("/icon/button/img_border_right_selected.png");
			
			
			leftSideImage = ImageLoader.load("/icon/button/bt_default_left_slice.png");
			middleSlice = ImageLoader.load("/icon/button/bt_default_middle_slice.png");
			rightSideImage = ImageLoader.load("/icon/button/bt_default_right_slice.png");
			leftSideImageClicked = ImageLoader.load("/icon/button/bt_default_selected_left_slice.png");
			middleSliceClicked = ImageLoader.load("/icon/button/bt_default_selected_middle_slice.png");
			rightSideImageClicked = ImageLoader.load("/icon/button/bt_default_selected_right_slice.png");
			
			leftSideImageDisable = ImageLoader.load("/icon/button/bt_default_disabled_left_slice.png");
			middleSliceDisable = ImageLoader.load("/icon/button/bt_default_disabled_middle_slice.png");
			rightSideImageDisable = ImageLoader.load("/icon/button/bt_default_disabled_right_slice.png");
			
			heightImage = leftSideImage.getHeight();
			
		} catch (Exception e) {
			
			log.error("setImageLayoutButton() - " + e);
			
		}
	}
	
	protected void setBackgroundButton(Graphics g, int x, int y, Image leftSideImage, Image middleSlice, Image rightSideImage, int colorLabel)
	{
		int i = 0;

		this.label.setTextColor(colorLabel);

		g.drawImage(leftSideImage, x, y, Graphics.TOP | Graphics.LEFT);

		try 
		{
			for (i = x + leftSideImage.getWidth(); i <= Display.getInstance().getWidth() - (rightSideImage.getWidth() + rightMargin); i++) 
			{
				g.drawImage(middleSlice, i, y, Graphics.TOP | Graphics.LEFT);
			}

			g.drawImage(rightSideImage, i, y, Graphics.TOP	| Graphics.LEFT);

			if (icon != null)
			{
				icon.paint(g, x + 10, y + 10, icon.getWidth(), icon.getHeight());
			}
			
			this.width = Display.getInstance().getWidth() - rightMargin - leftMargin;
			
		}
		catch (Exception e) 
		{
			log.error("setBackgroundButton - " + e);
		}
	}
	
	protected void setBorderButtonImage(Graphics g, int x, int y, Image leftBorderBackgroundImage, Image middleBorderBackgroundImage, Image rightBorderBackgroundImage)
	{
		int i = 0;

		g.drawImage(leftBorderBackgroundImage, x, y, Graphics.TOP | Graphics.LEFT);

		try 
		{
			int sizeButton = leftMargin + backgroundImage.getWidth() - rightBorderBackgroundImage.getWidth() ;
			for (i = x + leftBorderBackgroundImage.getWidth(); i <= sizeButton; i++) 
			{
				g.drawImage(middleBorderBackgroundImage, i, y, Graphics.TOP | Graphics.LEFT);
			}
			g.drawImage(rightBorderBackgroundImage, i, y, Graphics.TOP	| Graphics.LEFT);

		}
		catch (Exception e) 
		{
			log.error("setBorderButtonImage - " + e);
		}
	}
	
	public void click(int x, int y) {
		
		if(buttonActionListener != null) {
			if(!isDisabled()){
				buttonActionListener.buttonClicked(this);
			}
		}
		setPressedDownState(false);
	}
	
	public void longPressed(int x, int y) {
		click(x, y);
	}
	
	public void pressFire() {
		click(0, 0);
	}
	
	public String toString() {
		
		StringBuffer sb = new StringBuffer();
		
		sb.append("\nrightMargin:" + rightMargin);
		sb.append("\nleftMargin.:" + leftMargin);
		sb.append("\nwidth......:" + width);
		sb.append("\nheight.....:" + height);
		
		return sb.toString();
		
	}


	public void setButtonImagens(Image backgroundImage, Image selectedBackground) {
		this.backgroundImage = backgroundImage;
		this.selectedBackground = selectedBackground;
		calculateDimensions();
	}
	
	public void calculateDimensions()
	{
        if (backgroundImage != null) {
            height = backgroundImage.getHeight() + topMargin + bottomMargin;
            width = backgroundImage.getWidth() + rightMargin + leftMargin;
            int marginTop =  (backgroundImage.getHeight() - label.getFont().getHeight()) / 2;
            int marginLeft = (backgroundImage.getWidth() - label.getFont().stringWidth(label.getText())) / 2;
            label.setMargin(marginLeft, marginTop, 5, 7);
        }
	}
}